﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
/// <summary>
/// sqlhelper 的摘要说明
/// </summary>
public class sqlhelper
{

    private static readonly string constr = ConfigurationManager.ConnectionStrings["connectionstr"].ConnectionString;
    public static class SqlHelper
    {
        //获取连接字符串
       
       
    }
    public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
    {
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                if (pms != null)
                {
                    cmd.Parameters.AddRange(pms);
                }
                con.Open();
                return cmd.ExecuteNonQuery();
            } 
        }
    }
    public static object Executescalar(string sql, params SqlParameter[] pms)
    {
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                if (pms != null)
                {
                    cmd.Parameters.AddRange(pms);
                }
                con.Open();
                return cmd.ExecuteScalar();
            }
        }
    }
    public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms)
    {
        SqlConnection con = new SqlConnection(constr);

            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                if (pms != null)
                {
                    cmd.Parameters.AddRange(pms);
                }
                try
                {
                
                con.Open();
                    return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                con.Close();

            }
            
                catch (Exception)
                {
                    con.Close();
                    con.Dispose();
                    throw;
                }
           
        }
    }
    public static DataTable ExecuteDatatable(string sql, params SqlParameter[] pms)
    {
        DataTable dt = new DataTable();
        using (SqlDataAdapter adapter = new SqlDataAdapter(sql,constr))
        {
            
            
                if (pms != null)
                {
                    adapter.SelectCommand.Parameters.AddRange(pms);
                }
                adapter.Fill(dt);
                return dt;
            
        }
    }
	public sqlhelper()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}
}